package com.wpen.unit.infra.repository.job.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.wpen.unit.exception.UpdateZeroException;
import com.wpen.unit.infra.mybatis.job.mapper.BatchInfoMapper;
import com.wpen.unit.infra.mybatis.job.po.BatchInfoPo;
import com.wpen.unit.infra.mybatis.job.po.BatchInfoPo.BatchInfoColumns;
import com.wpen.unit.infra.mybatis.job.po.TaskInfoPo.TaskInfoColumns;
import com.wpen.unit.infra.repository.job.BatchInfoRepository;
import com.wpen.unit.utils.StringUtil;

/**
 * 批次信息维护.
 * 
 * @author pg
 *
 */
@Repository
@Transactional
public class BatchInfoRepositoryImpl extends ServiceImpl<BatchInfoMapper, BatchInfoPo> implements BatchInfoRepository{
    
    @Override
    public BatchInfoPo queryByCode(String bizCode, String batchCode) {
        QueryWrapper<BatchInfoPo> queryWrapper = Wrappers.query();
        queryWrapper.eq(TaskInfoColumns.BIZ_CODE.name(), bizCode);
        queryWrapper.eq(TaskInfoColumns.BATCH_CODE.name(), batchCode);
        return super.getOne(queryWrapper);
    }
    
    @Override
    public long count(BatchInfoPo batchInfoPo){
		QueryWrapper<BatchInfoPo> queryWrapper = createQuery(batchInfoPo);
		return super.count(queryWrapper);
    }

	@Override
    public List<BatchInfoPo> queryList(int index, int size, BatchInfoPo batchInfoPo){
        QueryWrapper<BatchInfoPo> queryWrapper = createQuery(batchInfoPo);
		if (size > 0) {
			Page<BatchInfoPo> page = new Page<>(index, size, false);
			return super.page(page, queryWrapper).getRecords();
		}
		//queryWrapper.orderBy(true, false, BatchInfoPoColumns.ID.name());
		return getBaseMapper().selectList(queryWrapper);
    }
    
    private QueryWrapper<BatchInfoPo> createQuery(BatchInfoPo batchInfoPo) {
        QueryWrapper<BatchInfoPo> queryWrapper = Wrappers.query();
        if (batchInfoPo.getId() != null) {
            queryWrapper.eq(BatchInfoColumns.ID.name(), batchInfoPo.getId());
        }
        if (StringUtil.isNotEmpty(batchInfoPo.getBizCode())) {
            queryWrapper.eq(BatchInfoColumns.BIZ_CODE.name(), batchInfoPo.getBizCode());
        }
        if (StringUtil.isNotEmpty(batchInfoPo.getBatchCode())) {
            queryWrapper.eq(BatchInfoColumns.BATCH_CODE.name(), batchInfoPo.getBatchCode());
        }
        if (StringUtil.isNotEmpty(batchInfoPo.getBatchType())) {
            queryWrapper.eq(BatchInfoColumns.BATCH_TYPE.name(), batchInfoPo.getBatchType());
        }
        if (StringUtil.isNotEmpty(batchInfoPo.getScheduleType())) {
            queryWrapper.eq(BatchInfoColumns.SCHEDULE_TYPE.name(), batchInfoPo.getScheduleType());
        }
        //if (StringUtil.isNotEmpty(batchInfoPo.getScheduleConf())) {
        //    queryWrapper.eq(BatchInfoColumns.SCHEDULE_CONF.name(), batchInfoPo.getScheduleConf());
        //}
        //if (batchInfoPo.getOrders() != null) {
        //    queryWrapper.eq(BatchInfoColumns.ORDERS.name(), batchInfoPo.getOrders());
        //}
        if (StringUtil.isNotEmpty(batchInfoPo.getEnabled())) {
            queryWrapper.eq(BatchInfoColumns.ENABLED.name(), batchInfoPo.getEnabled());
        }
        //if (StringUtil.isNotEmpty(batchInfoPo.getBatchDesc())) {
        //    queryWrapper.eq(BatchInfoColumns.BATCH_DESC.name(), batchInfoPo.getBatchDesc());
        //}
        if (batchInfoPo.getStartupMaxDelay() != null) {
            queryWrapper.eq(BatchInfoColumns.STARTUP_MAX_DELAY.name(), batchInfoPo.getStartupMaxDelay());
        }
        if (batchInfoPo.getExeTimeout() != null) {
            queryWrapper.eq(BatchInfoColumns.EXE_TIMEOUT.name(), batchInfoPo.getExeTimeout());
        }
        //if (StringUtil.isNotEmpty(batchInfoPo.getManagerP1())) {
        //    queryWrapper.eq(BatchInfoColumns.MANAGER_P1.name(), batchInfoPo.getManagerP1());
        //}
        //if (StringUtil.isNotEmpty(batchInfoPo.getManagerP2())) {
        //    queryWrapper.eq(BatchInfoColumns.MANAGER_P2.name(), batchInfoPo.getManagerP2());
        //}
        //if (StringUtil.isNotEmpty(batchInfoPo.getManagerP3())) {
        //    queryWrapper.eq(BatchInfoColumns.MANAGER_P3.name(), batchInfoPo.getManagerP3());
        //}
        if (batchInfoPo.getCreateTime() != null) {
            queryWrapper.eq(BatchInfoColumns.CREATE_TIME.name(), batchInfoPo.getCreateTime());
        }
        //if (batchInfoPo.getVersion() != null) {
        //    queryWrapper.eq(BatchInfoColumns.VERSION.name(), batchInfoPo.getVersion());
        //}
        return queryWrapper;
    }
    
    @Override
    public BatchInfoPo queryFirst(BatchInfoPo batchInfoPo) {
        QueryWrapper<BatchInfoPo> queryWrapper = Wrappers.query();
        if (batchInfoPo.getId() != null) {
            queryWrapper.eq(BatchInfoColumns.ID.name(), batchInfoPo.getId());
        }
        if (StringUtil.isNotEmpty(batchInfoPo.getBizCode())) {
            queryWrapper.eq(BatchInfoColumns.BIZ_CODE.name(), batchInfoPo.getBizCode());
        }
        if (StringUtil.isNotEmpty(batchInfoPo.getBatchCode())) {
            queryWrapper.eq(BatchInfoColumns.BATCH_CODE.name(), batchInfoPo.getBatchCode());
        }
        if (StringUtil.isNotEmpty(batchInfoPo.getBatchType())) {
            queryWrapper.eq(BatchInfoColumns.BATCH_TYPE.name(), batchInfoPo.getBatchType());
        }
        if (StringUtil.isNotEmpty(batchInfoPo.getScheduleType())) {
            queryWrapper.eq(BatchInfoColumns.SCHEDULE_TYPE.name(), batchInfoPo.getScheduleType());
        }
        //if (StringUtil.isNotEmpty(batchInfoPo.getScheduleConf())) {
        //    queryWrapper.eq(BatchInfoColumns.SCHEDULE_CONF.name(), batchInfoPo.getScheduleConf());
        //}
        //if (batchInfoPo.getOrders() != null) {
        //    queryWrapper.eq(BatchInfoColumns.ORDERS.name(), batchInfoPo.getOrders());
        //}
        if (StringUtil.isNotEmpty(batchInfoPo.getEnabled())) {
            queryWrapper.eq(BatchInfoColumns.ENABLED.name(), batchInfoPo.getEnabled());
        }
        //if (StringUtil.isNotEmpty(batchInfoPo.getBatchDesc())) {
        //    queryWrapper.eq(BatchInfoColumns.BATCH_DESC.name(), batchInfoPo.getBatchDesc());
        //}
        if (batchInfoPo.getStartupMaxDelay() != null) {
            queryWrapper.eq(BatchInfoColumns.STARTUP_MAX_DELAY.name(), batchInfoPo.getStartupMaxDelay());
        }
        if (batchInfoPo.getExeTimeout() != null) {
            queryWrapper.eq(BatchInfoColumns.EXE_TIMEOUT.name(), batchInfoPo.getExeTimeout());
        }
        //if (StringUtil.isNotEmpty(batchInfoPo.getManagerP1())) {
        //    queryWrapper.eq(BatchInfoColumns.MANAGER_P1.name(), batchInfoPo.getManagerP1());
        //}
        //if (StringUtil.isNotEmpty(batchInfoPo.getManagerP2())) {
        //    queryWrapper.eq(BatchInfoColumns.MANAGER_P2.name(), batchInfoPo.getManagerP2());
        //}
        //if (StringUtil.isNotEmpty(batchInfoPo.getManagerP3())) {
        //    queryWrapper.eq(BatchInfoColumns.MANAGER_P3.name(), batchInfoPo.getManagerP3());
        //}
        if (batchInfoPo.getCreateTime() != null) {
            queryWrapper.eq(BatchInfoColumns.CREATE_TIME.name(), batchInfoPo.getCreateTime());
        }
        //if (batchInfoPo.getVersion() != null) {
        //    queryWrapper.eq(BatchInfoColumns.VERSION.name(), batchInfoPo.getVersion());
        //}
		//queryWrapper.orderBy(true, false, BatchInfoPoColumns.ID.name());
		return super.getOne(queryWrapper);
    }
    
    @Override
    public BatchInfoPo queryById(Serializable id) {
        return super.getById(id);
    }
    
    @Override
    public boolean insert(BatchInfoPo batchInfoPo) {
        batchInfoPo.setVersion(1);
        batchInfoPo.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
        return super.save(batchInfoPo);
    }

    @Override
    public boolean insertBatch(List<BatchInfoPo> listBatchInfoPos) {
        int rs = getBaseMapper().insertBatchSomeColumn(listBatchInfoPos);
        return SqlHelper.retBool(rs);
    }

    @Override
    public boolean updateById(BatchInfoPo batchInfoPo) {
        UpdateWrapper<BatchInfoPo> updateWrapper = Wrappers.update();
        updateWrapper.eq(BatchInfoColumns.ID.name(), batchInfoPo.getId());
        updateWrapper.eq(BatchInfoColumns.VERSION.name(), batchInfoPo.getVersion());
        batchInfoPo.setVersion(batchInfoPo.getVersion() + 1);
        boolean rs = super.update(batchInfoPo, updateWrapper);
        // 根据ID更新所有字段，空值也更新
        // int num = getBaseMapper().alwaysUpdateSomeColumnById(BatchInfoPo);
        // boolean rs = SqlHelper.retBool(num);
        if (!rs) {
            throw new UpdateZeroException();
        }
        return rs;
    }

    @Override
    public boolean deleteByIds(Serializable[] ids) {
        UpdateWrapper<BatchInfoPo> updateWrapper = Wrappers.update();
		updateWrapper.in(BatchInfoColumns.ID.name(), (Object[]) ids);
		return super.remove(updateWrapper);
    }
    
//    public BatchInfoPo queryByExample(BatchInfoPo batchInfoPo) {
//        QueryWrapper<BatchInfoPo> queryWrapper = Wrappers.query();
//        if (batchInfoPo.getId() != null) {
//            queryWrapper.eq(BatchInfoColumns.ID.name(), batchInfoPo.getId());
//        }
//        if (StringUtil.isNotEmpty(batchInfoPo.getBizCode())) {
//            queryWrapper.eq(BatchInfoColumns.BIZ_CODE.name(), batchInfoPo.getBizCode());
//        }
//        if (StringUtil.isNotEmpty(batchInfoPo.getBatchCode())) {
//            queryWrapper.eq(BatchInfoColumns.BATCH_CODE.name(), batchInfoPo.getBatchCode());
//        }
//        if (StringUtil.isNotEmpty(batchInfoPo.getBatchType())) {
//            queryWrapper.eq(BatchInfoColumns.BATCH_TYPE.name(), batchInfoPo.getBatchType());
//        }
//        if (StringUtil.isNotEmpty(batchInfoPo.getScheduleType())) {
//            queryWrapper.eq(BatchInfoColumns.SCHEDULE_TYPE.name(), batchInfoPo.getScheduleType());
//        }
//        if (StringUtil.isNotEmpty(batchInfoPo.getScheduleConf())) {
//            queryWrapper.eq(BatchInfoColumns.SCHEDULE_CONF.name(), batchInfoPo.getScheduleConf());
//        }
//        if (batchInfoPo.getOrders() != null) {
//            queryWrapper.eq(BatchInfoColumns.ORDERS.name(), batchInfoPo.getOrders());
//        }
//        if (StringUtil.isNotEmpty(batchInfoPo.getEnabled())) {
//            queryWrapper.eq(BatchInfoColumns.ENABLED.name(), batchInfoPo.getEnabled());
//        }
//        if (StringUtil.isNotEmpty(batchInfoPo.getBatchDesc())) {
//            queryWrapper.eq(BatchInfoColumns.BATCH_DESC.name(), batchInfoPo.getBatchDesc());
//        }
//        if (batchInfoPo.getStartupMaxDelay() != null) {
//            queryWrapper.eq(BatchInfoColumns.STARTUP_MAX_DELAY.name(), batchInfoPo.getStartupMaxDelay());
//        }
//        if (batchInfoPo.getExeTimeout() != null) {
//            queryWrapper.eq(BatchInfoColumns.EXE_TIMEOUT.name(), batchInfoPo.getExeTimeout());
//        }
//        if (StringUtil.isNotEmpty(batchInfoPo.getManagerP1())) {
//            queryWrapper.eq(BatchInfoColumns.MANAGER_P1.name(), batchInfoPo.getManagerP1());
//        }
//        if (StringUtil.isNotEmpty(batchInfoPo.getManagerP2())) {
//            queryWrapper.eq(BatchInfoColumns.MANAGER_P2.name(), batchInfoPo.getManagerP2());
//        }
//        if (StringUtil.isNotEmpty(batchInfoPo.getManagerP3())) {
//            queryWrapper.eq(BatchInfoColumns.MANAGER_P3.name(), batchInfoPo.getManagerP3());
//        }
//        if (batchInfoPo.getAddTime() != null) {
//            queryWrapper.eq(BatchInfoColumns.ADD_TIME.name(), batchInfoPo.getAddTime());
//        }
//        if (batchInfoPo.getVersion() != null) {
//            queryWrapper.eq(BatchInfoColumns.VERSION.name(), batchInfoPo.getVersion());
//        }
//		Page<BatchInfoPo> page = new Page<>(0, 1, false);
//		queryWrapper.orderBy(true, false, BatchInfoPoColumns.ID.name());
//    }

}
